﻿using System;
using System.Text;
using wbServo.constant;

namespace wbServo.utils
{
    public class MyHexUtils
    {
        /// <summary>
        /// 根据ushort[]数组，截取指定长度并转换为字符串
        /// </summary>
        /// <param name="src"></param>
        /// <param name="start"></param>
        /// <param name="len"></param>
        /// <param name="typ"></param>
        /// <returns></returns>
        public static string GetString(ushort[] src, string typ)
        {
            switch (typ)
            {
                case "D":
                    return src[0].ToString();
                case "S":
                    byte[] bytes = new byte[src.Length * 2];
                    for (int i = 0; i < src.Length; i++)
                    {
                        bytes[i * 2] = (byte)(src[i] >> 8);     // 高字节
                        bytes[i * 2 + 1] = (byte)(src[i] & 0xFF); // 低字节
                    }
                    // 将字节数组转换为字符串（假设使用 ASCII 编码）
                    return Encoding.ASCII.GetString(bytes).TrimEnd('\0');
            }
            return "NAN";
        }

        public static ushort[] SetString(string value, int len, string typ)
        {
            ushort[] temp = new ushort[len];

            if ("D".Equals(typ))
            {
                temp[0] = Convert.ToUInt16(value);
            }
            else
            {
                byte[] bytes = Encoding.ASCII.GetBytes(value);
                for (int i = 0; i < len; i++)
                {
                    if (i * 2 < bytes.Length)
                    {
                        temp[i] = (ushort)((bytes[i * 2] << 8) | (i * 2 + 1 < bytes.Length ? bytes[i * 2 + 1] : 0));
                    }
                    else
                    {
                        temp[i] = 0; // 不足部分用 0 填充
                    }
                }
            }

            return temp;
        }

        /// <summary>
        /// 获取short类型数据
        /// </summary>
        /// <param name="src"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public static short GetShort(ushort[] src, int start)
        {
            ushort[] temp = new ushort[1];
            temp[0] = src[start];
            byte[] bytesTemp = Ushorts2Bytes(temp);
            short res = BitConverter.ToInt16(bytesTemp, 0);
            return res;
        }

        /// <summary>
        /// 将ushort[]转换为byte[]
        /// </summary>
        /// <param name="src"></param>
        /// <param name="reverse"></param>
        /// <returns></returns>
        private static byte[] Ushorts2Bytes(ushort[] src, bool reverse = false)
        {

            int count = src.Length;
            byte[] dest = new byte[count << 1];
            if (reverse)
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i * 2] = (byte)(src[i] >> 8);
                    dest[i * 2 + 1] = (byte)(src[i] >> 0);
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i * 2] = (byte)(src[i] >> 0);
                    dest[i * 2 + 1] = (byte)(src[i] >> 8);
                }
            }
            return dest;
        }

        /// <summary>
        /// 将byte[]转换为ushort[]
        /// </summary>
        /// <param name="src"></param>
        /// <param name="reverse"></param>
        /// <returns></returns>
        private static ushort[] Bytes2Ushorts(byte[] src, bool reverse = false)
        {
            int len = src.Length;

            byte[] srcPlus = new byte[len + 1];
            src.CopyTo(srcPlus, 0);
            int count = len >> 1;

            if (len % 2 != 0)
            {
                count += 1;
            }

            ushort[] dest = new ushort[count];
            if (reverse)
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
                }
            }

            return dest;
        }
    }
}
